{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "词典中的最大词长： 5\n",
      "正向最大匹配法FMM，分词结果： ['南京市长', '江', '大桥', '非常宏伟']\n",
      "逆向最大匹配法RMM，分词结果： ['南京市', '长江大桥', '非常宏伟']\n",
      "双向最大匹配法BMM，分词结果： ['南京市', '长江大桥', '非常宏伟']\n"
     ]
    }
   ],
   "source": [
    "import string, re\n",
    "def load_dict(filename):\n",
    "    f = open(filename, 'r', encoding = 'utf8').read()\n",
    "    maxLen = 1\n",
    "    dict = f.split(\"\\n\")\n",
    "    for i in dict:\n",
    "        if len(i)>maxLen:\n",
    "            maxLen = len(i)\n",
    "    return dict, maxLen;\n",
    "def FMM(dict, maxLen, text):\n",
    "    cut_word = []\n",
    "    start = 0\n",
    "    textLen = len(text)\n",
    "    while start != textLen:\n",
    "        index = start + maxLen\n",
    "        if index > len(text):\n",
    "        index = len(text)\n",
    "        for i in range(maxLen):\n",
    "            if(text[start:index] in dict)or(len(text[start:\n",
    "index]) == 1):\n",
    "                cut_word.append(text[start:index])\n",
    "                start = index  5\n",
    "                break\n",
    "            index -= 1   \n",
    "    return cut_word\n",
    "def RMM(dict, maxLen, text):\n",
    "    cut_word = []\n",
    "    textLen = len(text)\n",
    "    while textLen > 0:\n",
    "        j = 0\n",
    "        for size in range(maxLen, 0, -1):\n",
    "            \n",
    "            if textLen < size:\n",
    "                continue\n",
    "            cutword = text[textLen-size:textLen]\n",
    "            if cutword in dict:\n",
    "                cut_word.append(cutword)\n",
    "                textLen -=\n",
    "                j += 1\n",
    "                break\n",
    "        if j == 0:\n",
    "            textLen -= 1\n",
    "    cut_word = list(reversed(cut_word))\t\t\t\t#反转列表\n",
    "    return cut_word\n",
    "def BMM(dict, maxLen, text): \t\t\t#定义双向最大匹配函数\n",
    "    fmm = FMM(dict, maxLen, text)\t\t#调用正向最大匹配函数\n",
    "    rmm = RMM(dict, maxLen, text)\n",
    "\n",
    "\tif len(fmm) != len(rmm): \n",
    "\t\tif len(fmm) < len(rmm):\n",
    "\t\t\treturn fmm\n",
    "\t\telse:\n",
    "\t\t\treturn rmm\n",
    "\telse:\t\t\t\t\t\t\t#如果分词数量相等\n",
    "\t\tif fmm == rmm:\t\t\t#如果分词结果完全一致，随机选择一组\n",
    "\t\t\treturn fmm\n",
    "\t\telse:\t\t\t\t#如果分词结果不一致，选择单字数量较少的一组\n",
    "\t\t\tfmm_1num = len ([i for i in fmm if len(i) == 1])\n",
    "\t\t\trmm_1num = len ([i for i in rmm if len(i) == 1])\n",
    "\t\t\treturn fmm_1num if fmm_1num < rmm_1num else rmm_1num\n",
    "def main():\t\t\t\t#主函数\n",
    "\tdict,maxLen = load_dict('data/dict1.txt')#路径\n",
    "\tprint(\"词典中的最大词长：\", maxLen)\n",
    "\ttext = \"南京市长江大桥非常宏伟\"\n",
    "\tfmm_cut = FMM(dict, maxLen, text)\n",
    "\tprint(\"正向最大匹配法FMM，分词结果：\", fmm_cut)\n",
    "\trmm_cut = RMM(dict, maxLen, text)\n",
    "\tprint(\"逆向最大匹配法RMM，分词结果：\", rmm_cut)\n",
    "\tbmm_cut = BMM(dict, maxLen, text)\n",
    "\tprint(\"双向最大匹配法BMM，分词结果：\", bmm_cut)\n",
    "if __name__ == '__main__':  程序入口\n",
    "\tmain()     #调用函数   函数名()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
